<html>
<head>
<script type="text/javascript">
function showHint(str)
{
  if (str.length==0)
  {
    document.getElementById("textHint").innerHTML="";
    return;
  }
  if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }
  else
  {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      document.getElementById("textHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","get_hint.php?q="+str, true);
  xmlhttp.send();
}
</script>
</head>
<body>

<? 
$text = $_REQUEST["ftext"];

$text = htmlspecialchars(stripslashes($text));

if (strlen($text) > 0)
{

  $text = substr($text, 0, 50);

  $con = mysql_connect("localhost","user","mypass");
  if (!$con)
  {
    echo 'Could not connect: ' . mysql_error();
  }
  else
  {
    mysql_select_db("db1", $con);

    $sql = "INSERT INTO table1 (id, text) VALUES (NULL, '" . $text . "') ";
    if (!mysql_query($sql,$con))
    {
      echo "Text hasn't been added. " . mysql_error() . "<br/>";
    }
    else
    {
      echo "Text <img src='get_image.php?q=" . $text . "'/> added successfully! <br/>";
    }
  }
  mysql_close($con);
}
?>
<br/>

<form action="index.php" method="post">
Enter text: <input type="text" name="ftext" value="<? echo $_REQUEST["ftext"]; ?>" onkeyup="showHint(this.value)" />
<input type="submit" value="Add"/>
</form>

<p>Suggestions: <span id="textHint"></span></p>

</body>
</html>
